﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Platform.File.Models;
using MiniFox.Platform.File.Providers;
using MiniFox.Utility;
using Platform.Core.Providers;
using System.Linq.Expressions;

namespace Platform.Data
{
    [AutoResolve]
    public class GeneraicStorageProvider : GeneraicFileProvider, IStorageProvider
    {
        public bool Accept(FileData fileData)
        {
            return Save(fileData);
        }

        public FileData LoadPreSave(string fileId)
        {
            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.FileId == fileId && f.Accepted == false);
            if (file == null) return null;
            return new FileData
            {
                FileId = fileId,
                FileName = file.FileName,
                Path = file.Path,
                ContentType = file.MimeType,
                Group = file.Group,
                CreateTime = file.CreateTime,
                UpdateTime = file.UpdateTime
            };
        }

        public bool PreSave(FileData[] fileDatas)
        {
            List<BatchOperationItem> list = new List<BatchOperationItem>();
            Parallel.ForEach(fileDatas, fileData =>
            {
                var file = new Entities.MySql.File
                {
                    FileId = fileData.FileId,
                    FileName = fileData.FileName,
                    ExtName = fileData.ExtensionName,
                    MimeType = fileData.ContentType,
                    ContentLength = fileData.ContentLength,
                    Group = fileData.Group,
                    Path = fileData.Path,
                    CreateTime = fileData.CreateTime,
                    UpdateTime = fileData.UpdateTime,
                    Accepted = false
                };
                list.Add(new BatchOperationItem
                {
                    Entity = file,
                    State = Microsoft.EntityFrameworkCore.EntityState.Added
                });
            });
            var len = fileDatas.Length;
            return this.CommitBatch(list) >= len;
        }

        public bool Remove(string fileId)
        {
            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.FileId == fileId);

            return this.Delete(file) > 0;
        }

        public bool Rename(string group, string fileId, string newName)
        {
            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.Group == group && f.FileId == fileId);
            file.FileName = newName;
            file.UpdateTime = DateTime.Now;
            return this.Update(file) > 0;
        }

        public bool Save(FileData fileData)
        {
            if (fileData == null) return false;

            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.FileId == fileData.FileId);
            if (file == null)
            {
                file = new ()
                {
                    FileId = fileData.FileId,
                    FileName = fileData.FileName,
                    ExtName = fileData.ExtensionName,
                    MimeType = fileData.ContentType,
                    ContentLength = fileData.ContentLength,
                    Group = fileData.Group,
                    Path = fileData.Path,
                    CreateTime = fileData.CreateTime,
                    UpdateTime = fileData.UpdateTime,
                    Accepted = true
                };
                return this.Insert(file) > 0;
            }
            else
            {
                file.UpdateTime = fileData.UpdateTime;
                file.Accepted = true;
                return this.Update(file) > 0;
            }
        }
    }
}
